package com.dawning.utiming.task.server

import android.app.Activity
import android.content.Context
import android.os.Handler
import android.support.v4.app.Fragment
import android.text.TextUtils
import android.view.MotionEvent
import android.view.View
import android.widget.EditText
import cc.vv.baselibrary.util.ColorUtil
import cc.vv.baselibrary.util.LKPrefUtil
import cc.vv.baselibrary.util.router.RouterIntentUtil
import cc.vv.baselibrary.view.JniCancelAndConfirmDialog
import cc.vv.baselibrary.view.imgselector.ImageSelector
import cc.vv.baselibrary.view.imgselector.utils.LKPermissionUtil
import cc.vv.btpartyjob.partymanager.R
import java.util.*

/**
 * 选择图片或者视频的Server
 * Created by chen.qi on 2018/9/26.
 */
object SubmitJobServer {

    /**
     * 删除的key
     */
    var delKey: String? = ""

    /**
     * 是否继续上传，返回false继续上传，返回true取消上传
     */
    private var isUploading: Boolean = false

    /**
     * 发布作业可以选择图片和视频文件
     */
    fun openPic(activity: Activity, fragment: Fragment, max: Int) {
        val carmaSDPermission = LKPermissionUtil.getInstance().requestCarmaSDPermission(activity)
        if (carmaSDPermission) {
            val imagTag = LKPrefUtil.getBoolean("imagTag", false)
            if (imagTag) {
                setClick(fragment, max, true)
            } else {
                setDialog(activity, fragment, max, true)
            }
        }
    }


    /**
     * 去相册获取图片
     */
    private fun setClick(fragment: Fragment, max: Int, selectVideo: Boolean) {
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(max)
                .setGridColumns(4)
                .setShowCamera(true)
                .setShowVideo(selectVideo)
                .startSelectPic(fragment)
    }

    /**
     * 拦截dialog
     */
    private fun setDialog(activity: Activity, fragment: Fragment, max: Int, selectVideo: Boolean) {
        val bcCancelAndConfirmDialog = JniCancelAndConfirmDialog(activity)
        //设置标题
        bcCancelAndConfirmDialog.setTitle("\"佑时光\"想访问你的照片")
        bcCancelAndConfirmDialog.setTitleColor(ColorUtil.setBCColor(R.color.color_484A4D))
        bcCancelAndConfirmDialog.setTitleSize(16)
        //设置内容
        bcCancelAndConfirmDialog.setContent("是否允许访问你的相册?")
        bcCancelAndConfirmDialog.setContentColor(ColorUtil.setBCColor(R.color.color_9C9EA3))
        bcCancelAndConfirmDialog.setContextSize(14f)
        //设置是否字体
        bcCancelAndConfirmDialog.confirmBtnText = "允许"
        bcCancelAndConfirmDialog.cancelBtnText = "不允许"
        bcCancelAndConfirmDialog.setEventInterface(object : JniCancelAndConfirmDialog.EventInterface {
            override fun cancelOnClick() {}

            override fun confirmOnClick() {
                setClick(fragment, max, selectVideo)
                LKPrefUtil.putBoolean("imagTag", true)
            }
        })
        if (!bcCancelAndConfirmDialog?.isShowing) {
            bcCancelAndConfirmDialog.show()
        }
    }

    /**
     * 提交拦截的dialog
     */
    fun submitDialog(activity: Activity, handler: Handler, msg: Int, title: String, content: String) {
        val bcCancelAndConfirmDialog = JniCancelAndConfirmDialog(activity)
        //设置标题
        bcCancelAndConfirmDialog.setTitle(title)
        bcCancelAndConfirmDialog.setTitleColor(ColorUtil.setBCColor(R.color.color_484A4D))
        bcCancelAndConfirmDialog.setTitleSize(16)
        //设置内容
        bcCancelAndConfirmDialog.setContent(content)
        bcCancelAndConfirmDialog.setContentColor(ColorUtil.setBCColor(R.color.color_9C9EA3))
        bcCancelAndConfirmDialog.setContextSize(14f)
        //设置是否字体
        bcCancelAndConfirmDialog.confirmBtnText = "确定"
        bcCancelAndConfirmDialog.cancelBtnText = "取消"
        bcCancelAndConfirmDialog.setEventInterface(object : JniCancelAndConfirmDialog.EventInterface {
            override fun cancelOnClick() {
            }

            override fun confirmOnClick() {
                handler.sendEmptyMessage(msg)
            }
        })
        if (!bcCancelAndConfirmDialog.isShowing) {
            bcCancelAndConfirmDialog.show()
        }
    }


    /**
     * 是否取消上传
     */
    private fun isCancelUpload(keyStr: String): Boolean {
        if (!TextUtils.isEmpty(keyStr)) {
            if (keyStr == delKey) {
                isUploading = true
                delKey = ""
            } else {
                isUploading = false
            }
        } else {
            isUploading = false
        }
        return isUploading
    }


    /**
     * 生成6位数随机数
     */
    private fun getRandomNumber(): String {
        var rand = Random()//生成随机数
        var buf = StringBuffer()
        for (i in 0 until 6) {
            buf.append(rand.nextInt(10))
        }
        return buf.toString()
    }

    /**
     * 查看大图
     */
    fun largePic(context: Context, list: ArrayList<String>, position: Int) {
        RouterIntentUtil.getInstance().intentImageWatcherActivity(context, list, position)
    }


    /**
     * 点击区域是否在文本区
     *
     * @param v
     * @param event
     * @return
     */
    fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
        if (v != null && v is EditText) {
            val leftTop = intArrayOf(0, 0)
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop)
            val left = leftTop[0]
            val top = leftTop[1]
            val bottom = top + v.height
            val right = left + v.width
            return !(event.x > left && event.x < right
                    && event.y > top && event.y < bottom)
        }
        return false
    }


    /**
     * 时间转化
     */
    fun minTimeTrans(seconds: Int): String {
        val minStr: String
        val secStr: String
        val minutes = seconds / 60
        val remainingSeconds = seconds % 60

        if (seconds >= 3600) {
            return "已录音1小时"
        } else {

            minStr = when {
                minutes == 0 -> "00"
                minutes < 10 -> "0" + minutes
                else -> "$minutes"
            }
            secStr = when {
                remainingSeconds == 0 -> "00"
                remainingSeconds < 10 -> "0" + remainingSeconds
                else -> "$remainingSeconds"
            }
        }
        return "已录音" + minStr + "分钟" + secStr + "秒"
    }


    /**
     * 发布作业可以选择图片和视频文件
     */
    fun openPic(activity: Activity, max: Int) {
        val carmaSDPermission = LKPermissionUtil.getInstance().requestCarmaSDPermission(activity)
        if (carmaSDPermission) {
            val imagTag = LKPrefUtil.getBoolean("imagTag", false)
            if (imagTag) {
                setClick(activity, max, true)
            } else {
                setDialog(activity, max, true)
            }
        }
    }


    /**
     * 去相册获取图片
     */
    private fun setClick(activity: Activity, max: Int, selectVideo: Boolean) {
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(max)
                .setGridColumns(4)
                .setShowCamera(true)
                .setShowVideo(selectVideo)
                .startSelectPic(activity)
    }

    /**
     * 拦截dialog
     */
    private fun setDialog(activity: Activity, max: Int, selectVideo: Boolean) {
        val bcCancelAndConfirmDialog = JniCancelAndConfirmDialog(activity)
        //设置标题
        bcCancelAndConfirmDialog.setTitle("\"佑时光\"想访问你的照片")
        bcCancelAndConfirmDialog.setTitleColor(ColorUtil.setBCColor(R.color.color_484A4D))
        bcCancelAndConfirmDialog.setTitleSize(16)
        //设置内容
        bcCancelAndConfirmDialog.setContent("是否允许访问你的相册?")
        bcCancelAndConfirmDialog.setContentColor(ColorUtil.setBCColor(R.color.color_9C9EA3))
        bcCancelAndConfirmDialog.setContextSize(14f)
        //设置是否字体
        bcCancelAndConfirmDialog.confirmBtnText = "允许"
        bcCancelAndConfirmDialog.cancelBtnText = "不允许"
        bcCancelAndConfirmDialog.setEventInterface(object : JniCancelAndConfirmDialog.EventInterface {
            override fun cancelOnClick() {}

            override fun confirmOnClick() {
                setClick(activity, max, selectVideo)
                LKPrefUtil.putBoolean("imagTag", true)
            }
        })
        if (!bcCancelAndConfirmDialog?.isShowing) {
            bcCancelAndConfirmDialog.show()
        }
    }


}